package com.cnfangmao.masterdata.controller;


import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.common.util.ModelUtils;
import com.cnfangmao.masterdata.common.util.SnowFlakeUtils;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.dto.PermissionDto;
import com.cnfangmao.masterdata.dto.PermissionSearchDto;
import com.cnfangmao.masterdata.model.Permission;
import com.cnfangmao.masterdata.service.PermissionService;
import com.cnfangmao.masterdata.vo.GeneralPageVo;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author chenxd
 * @since 2020-04-17
 */
@RequestMapping("/permission")
@Api(tags="0-4、配置-权限管理")
@RestController
public class PermissionController {

	private static final  Logger logger = LoggerFactory.getLogger(PermissionController.class);
	
	@Autowired
	private PermissionService permissionService;
	
	@PostMapping(value="/list.do")
	@ApiOperation(value="权限列表")
	@ApiImplicitParam(name="dto",value="查询权限列表条件",paramType="body",required=true,dataType="GeneralSearchDto«PermissionSearchDto»")
	public ResBody<GeneralPageVo<Permission>> listPermission(@RequestBody GeneralSearchDto<PermissionSearchDto> dto) {
		ResBody<GeneralPageVo<Permission>> resBody =new ResBody<GeneralPageVo<Permission>>();
		try {
			IPage<Permission> permission =permissionService.queryListPermission(dto);
			GeneralPageVo<Permission> result= new GeneralPageVo<Permission>();
			result.setPageNo(permission.getCurrent());
			result.setPageSize(permission.getPages());
			result.setRecords(permission.getRecords());
			result.setTotalCount(permission.getTotal());
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.data(result);
			resBody.message("查询成功");
		} catch (Exception e) {
			logger.error(String.format("run list error in PermissionController: %s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;		
	}
	
	@PostMapping(value="/create.do")
	@ApiOperation(value="创建权限信息")
	@ApiImplicitParam(name="dto",value="创建权限信息",paramType="body",required=true,dataType="PermissionDto")
	public ResBody<Boolean> createPermission(@RequestBody PermissionDto dto) {
		ResBody<Boolean> resBody =new ResBody<Boolean>();
		try {
			if(dto.getPermissionCode().isEmpty() || dto.getPermissionName().isEmpty())
			{
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("权限编码或者权限名称为空");
				return resBody;
			}
			if(permissionService.findByPermissionCode(dto.getPermissionCode())!=null)
			{
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("权限编码已经存在");
				return resBody;
			}
			if(permissionService.findByPermissionName(dto.getPermissionName())!=null)
			{
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("权限名称已经存在");
				return resBody;
			}
			dto.setPermissionId(SnowFlakeUtils.generator());
			Permission permission =ModelUtils.parseObject(dto, Permission.class, "create");
			permissionService.save(permission);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("创建成功");
			resBody.data(true);			
		} catch (Exception e) {
			logger.error(String.format("run create error in PermissionController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	@PostMapping("/update.do")
	@ApiOperation(value="修改权限信息")
	@ApiImplicitParam(name="dto",value="修改权限信息",paramType="body",required=true,dataType="PermissionDto")
	public ResBody<Boolean> updatePermission(@RequestBody PermissionDto dto) {
		ResBody<Boolean> resBody = new ResBody<Boolean>();
		try {
			if(dto.getPermissionCode().isEmpty() || dto.getPermissionName().isEmpty())
			{
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("权限编码或者权限名称为空");
				return resBody;
			}
			Permission permissionCode=permissionService.findByPermissionCode(dto.getPermissionCode());
			if(permissionCode!=null && !permissionCode.getPermissionId().equals(dto.getPermissionId()))
			{
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("权限编码已经存在");
				return resBody;
			}
			Permission permissionName=permissionService.findByPermissionName(dto.getPermissionName());
			if(permissionName!=null && !permissionName.getPermissionId().equals(dto.getPermissionId()))
			{
				resBody.code(ResCodeEnum.FAIL.getCode());
				resBody.message("权限名称已经存在");
				return resBody;
			}
			Permission permission =ModelUtils.parseObject(dto, Permission.class, "update");
			permissionService.updateById(permission);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("修改成功");
			resBody.data(true);
		} catch (Exception e) {
			logger.error(String.format("run update error in PermissionController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	@PostMapping(value="/delete.do")
	@ApiOperation(value="删除权限信息")
	@ApiImplicitParam(name="permissionId",value="删除权限信息 permissionId",paramType="body",required=true,dataType="string")
	public ResBody<Boolean> deletePermission(@RequestBody String permissionId) {
		ResBody<Boolean> resBody = new ResBody<Boolean>();
		try {
			permissionService.removeById(permissionId);
			resBody.code(ResCodeEnum.SUCCESS.getCode());
			resBody.message("删除成功");
			resBody.data(true);
		} catch (Exception e) {
			logger.error(String.format("run delete error in PermissionController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	@PostMapping(value="/getPermission.do")
	@ApiOperation(value="获取权限信息")
	@ApiImplicitParam(name="permissionId",value="获取权限信息 permissionId",paramType="body",required=true,dataType="string")
	public ResBody<Permission> getPermission(@RequestBody String permissionId) {
		ResBody<Permission> resBody = new ResBody<Permission>();
		try {
			Permission permission =permissionService.getById(permissionId);
          	resBody.code(ResCodeEnum.SUCCESS.getCode());
    		resBody.data(permission);
		} catch (Exception e) {
			logger.error(String.format("run getPermission error in PermissionController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
		return resBody;
	}
	
	/**
     * 获取所有权限数据
     */
    @RequestMapping(value="/getAllPermission.do", method = RequestMethod.GET)
    @ApiOperation(value="获取所有权限数据", notes = "")
    public ResBody<List<PermissionDto>> getAllPermission(){
    	ResBody<List<PermissionDto>> resBody = new ResBody<List<PermissionDto>>();
    	try {
        	List<PermissionDto> permission =permissionService.getAllPermissions();
          	resBody.code(ResCodeEnum.SUCCESS.getCode());
    		resBody.data(permission);
		} catch (Exception e) {
			logger.error(String.format("run getAllPermission error in AccountBasicDataRightController：%s",e.getMessage()));
			resBody.code(ResCodeEnum.FAIL.getCode());
			resBody.message(e.getMessage());
		}
    	return resBody;
    }
}

